package com.lagou.email.lagouemail.service.impl;

import com.lagou.email.lagouemail.dao.LagouAuthCodeDao;
import com.lagou.email.lagouemail.entity.LagouAuthCode;
import com.lagou.email.lagouemail.service.LagouAuthCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
@Service
public class LagouAuthCodeServiceImpl implements LagouAuthCodeService {

    @Autowired
    private LagouAuthCodeDao lagouAuthCodeDao;

    @Override
    public void insertCode(String email, String code){
        LagouAuthCode lagouAuthCode=new LagouAuthCode();
        lagouAuthCode.setCode(code);
        lagouAuthCode.setEmail(email);
        Long time=System.currentTimeMillis();
        lagouAuthCode.setCreatetime(new Date(time));
        lagouAuthCode.setExpiretime(new Date(time+10*60*1000));

        lagouAuthCodeDao.insertEmailCode(lagouAuthCode);
    }

    @Override
    public boolean verifyCOde(String email, String verifyCode){
        LagouAuthCode lagouAuthCode=new LagouAuthCode();
        lagouAuthCode.setCode(verifyCode);
        lagouAuthCode.setEmail(email);
         LagouAuthCode byEmailCode = lagouAuthCodeDao.findByEmailCode(lagouAuthCode);
         if (byEmailCode!=null){
           return true;
         }
         return false;
    }
}
